package 刷题10月.每日一题0930;
// https://leetcode-cn.com/problems/license-key-formatting/
public class 每日一题482密钥格式化1004 {
    class Solution {
        public  String licenseKeyFormatting(String s, int k) {
            StringBuilder sb = new StringBuilder();
            int i = s.length()-1, count = 0; // count: 当前分组已填入多少个字符
            while (i >= 0) {
                char c = s.charAt(i--);
                if (c != '-') {
                    if (count == k) { // 当前分组字符数已满，新开一组
                        sb.append('-');
                        count = 0;
                    }
                    sb.append(isNumber(c) ? c : toUpper(c));
                    count++;
                }
            }
            return sb.reverse().toString();
        }

        private  Boolean isNumber(char c) {
            return c <= '9' && c >= '0';
        }

        private  char toUpper(char c) {
            if (c <= 'Z' && c >= 'A') return c;
            return (char)(c - 32);
        }
    }
}
